<template></template>
<script>
import Base64 from "@/api/base64";
export default {
  name: "MESPreview",
  data() {
    return {};
  },
  created() {
    /** 30 预览 **/
    let previewUrl = this.$route.query.url;
    var strLength = previewUrl.length;
    var str = previewUrl.slice(previewUrl.indexOf(".") + 1, strLength);
    let previewParam = {};
    var url = previewParameter.preview_path;
    previewParam.url = previewParameter.server_url_port;
    previewParam.accesskey = previewParameter.access_key;
    previewParam.secretkey = previewParameter.secret_key;
    previewParam.bucket = str;
    var index = previewUrl.lastIndexOf("/");
    var bucket = previewUrl.substring(0, index);
    var file = previewUrl.substring(index + 1, previewUrl.length);
    previewParam.bucket = bucket;
    previewParam.file = file;
    //水印
    var watermark = this.$store.state.user.name;

    let xhr = new XMLHttpRequest();
    xhr.open("POST", url, true); //建立间接，要求异步响应
    xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); //设置为表单方式提交
    xhr.onreadystatechange = () => {
      //绑定响应状态事件监听函数
      if (xhr.readyState == 4) {
        //监听readyState状态
        if (xhr.status == 200 || xhr.status == 0) {
          //监听HTTP状态码
          if (xhr.responseText === "" || xhr.responseText == null) {
            Loading.close();
            close = true;
            var rdata = JSON.parse(xhr.responseText);
            if (rdata.success) {
              // window.open(
              //   rdata.viewurl,
              //   "previewwindow",
              //   `height=${screenHeight},
              //               width=${screenWidth},
              //               top=${0.4 * screenHeight},
              //               left=${0.5 * screenWidth}`
              // );
              location.href = rdata.viewurl
            } else {
              this.msgError(rdata.msg);
            }
          } else {
            Loading.close();
            close = true;
            this.msgError("预览文件错误");
          }
        }
      }
    };
    let data = Base64.encode(previewParam);
    xhr.send(
      "file=" +
        encodeURIComponent(data) +
        "&watermark=" +
        watermark +
        "&cache=false"
    ); //发送请求
    /** 30 预览 **/
  },
};
</script>
